package com.rmondjone.view

import android.content.Intent
import android.net.http.SslError
import android.os.Build
import android.webkit.*
import com.rmondjone.androidcamerademo.R
import com.rmondjone.utils.setClickListener
import kotlinx.android.synthetic.main.dialog_web.*

/**
 * copyright：
 * @author：anthui creation date： .
 * @since: 1.0.0
 * @describe:
 */
class WebDialog : BaseDialog() {

//    lateinit var mProgression: ProgressBar

    lateinit var url: String
    override fun initComponents() {


    }


    private fun initWebView() {
        val setting: WebSettings = mWebView.settings
        // 支持缩放
        // 支持缩放
        setting.setSupportZoom(false)
        // 设置支持缩放 + -
        // 设置支持缩放 + -
        setting.builtInZoomControls = true
        // 关闭 webView 中缓存
        /**/
        /*webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);*/
        // 关闭 webView 中缓存
        /**/
        setting.useWideViewPort =
                true
        setting.loadWithOverviewMode = true
        // 设置WebView属性，能够执行Javascript脚本
        // 设置WebView属性，能够执行Javascript脚本
        setting.javaScriptEnabled = true
        setting.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
        setting.savePassword = false
        setting.domStorageEnabled = true
        setting.allowFileAccess = true
        setting.defaultTextEncodingName = "utf-8"
        // 设置UserAgent
        // 设置UserAgent
        setting.blockNetworkImage = false // 解决图片不显示

        setting.displayZoomControls = false

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setting.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
        }


        mWebView.webViewClient = object : WebViewClient() {
            override fun onReceivedSslError(
                    view: WebView?,
                    handler: SslErrorHandler, error: SslError?
            ) {
                handler.proceed() // 接受证书
            }

            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {

                if (url.isNullOrEmpty()) {
                    return false
                }
                try {
                    // 处理自定义scheme协议
                    if (!url.startsWith("http") && !url.startsWith("https")) {
                        return try {
                            val intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME)
                            intent.flags =
                                    Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP
                            intent.addCategory("android.intent.category.BROWSABLE")
                            intent.component = null
                            if (null != intent) {
                                startActivity(intent)
                            }
                            true
                        } catch (e: Exception) {
                            // 没有安装的情况
                            e.printStackTrace()
                            //                            ToastUitl.showShort("尚未安装此应用！");
                            true
                        }
                    }

                } catch (e: java.lang.Exception) {
                    e.printStackTrace()
                }
                return super.shouldOverrideUrlLoading(view, url)

            }


        }

        mWebView.webChromeClient = object : WebChromeClient() {
            override fun onReceivedTitle(view: WebView?, title: String?) {
                super.onReceivedTitle(view, title)
                if (title.isNullOrEmpty()) {
                    return
                }
//                tv_title.text = title
            }

            override fun onProgressChanged(
                    view: WebView?,
                    newProgress: Int
            ) {
                super.onProgressChanged(view, newProgress)
                if (newProgress == 100) {
//                    mProgression.visibility = View.GONE
                } else {
//                    mProgression.visibility = View.VISIBLE
//                    mProgression.progress = newProgress //设置加载进度
                }
            }

        }
        mWebView.setDownloadListener { url, userAgent, contentDisposition, mimetype, contentLength ->

        }
    }

    override fun initData() {

        layout_click.setClickListener {
            dismiss()
        }
        initWebView()

        iv_back.setClickListener { dismiss() }
        mWebView.loadUrl(url)

    }

    override fun getMainContentViewId(): Int {
        setOutCancel(true)

        return R.layout.dialog_web

    }

}